package linkedList;

import linkedList.entity.ListNode;

/**
 * @program: leetcode
 * @description: 自我实现尾插，头插，增加指定位置元素，删除指定位置元素，获取指定位置元素
 * @author: lxf
 * @create: 2024-03-15 17:18
 **/
public class OwnAllMethodSum {
    public static void main(String[] args) {
        tailInsert(1);
        tailInsert(2);
        tailInsert(3);
        tailInsert(4);
        headInsert(0);
        ListNode listNode1 = CirReturnHead(head);
        Integer byIndex = getByIndex(0);
        insertByIndex(0,-1);
        ListNode listNode = copyByCir(head);
        deleteByIndex(5);
        System.out.println(1);
    }

    static ListNode head = null;
    static int size = 0;

    public static void tailInsert(int num) {
        ListNode node = new ListNode(num);
        ListNode tmp = head;
        if (tmp == null){
            head = node;
            size++;
            return;
        }
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = node;
        size++;
    }

    public static void headInsert(int num) {
        ListNode node = new ListNode(num);
        node.next = head;
        head = node;
        size++;
    }

    public static Integer getByIndex(int index) {
        if (index > size || index < 0) {
            System.out.println("超过限制");
        }
        ListNode tmp = head;
        for (int i = 0; i < index; i++) {
            tmp = tmp.next;
        }
        return tmp.val;
    }

    public static void insertByIndex(int index, int num) {
        ListNode tmp = head;
        ListNode node = new ListNode(num);
        for (int i = 0; i < index - 1; i++) {
            tmp = tmp.next;
        }
        if (index == 0){
            headInsert(num);
            return;
        }
        node.next = tmp.next;
        tmp.next = node;
        size++;
    }

    public static void deleteByIndex(int index){
        ListNode tmp = head;
        for (int i = 0; i < index - 1 -1; i++) {
            tmp = tmp.next;
        }
        if (index == 0){
            deleteFromHead();
            return;
        }
        ListNode next = tmp.next;
        tmp.next = tmp.next.next;
        next.next = null;
        size--;
    }

    public static void deleteFromHead(){
        ListNode next = head.next;
        head.next = null;
        head = next;
        size--;
    }
    public static void deleteFromTail(){
        ListNode tmp = head;
        for (int i = 0; i < size - 1; i++) {
            tmp = tmp.next;
        }
        ListNode next = tmp.next;
        tmp.next = tmp.next.next;
        next.next = null;
        size--;
    }

    public static ListNode copyByCir(ListNode head){
        if (head == null){
            return null;
        }
        ListNode node = new ListNode(head.val);
        node.next = copyByCir(head.next);
        return node;
    }

    public static ListNode CirReturnHead(ListNode head){
        if (head == null){
            return null;
        }
        ListNode node = head;
        CirReturnHead(head.next);
        return node;
    }

}
